﻿using System;
using System.Windows.Input;

namespace UserInterface
{
    public class SimpleCommand : ICommand
    {
        public SimpleCommand(Action<object> action, string text = null)
        {
            if (action == null) throw new ArgumentNullException("action");
            _Action = action;
            _Text = text;
        }

        public bool CanExecute
        {
            get { return _canExecute; }
            set 
            {
                if (value == _canExecute) return;
                _canExecute = value;
                RaiseCanExecuteChanged();
            }
        }

        public string Text
        {
            get { return _Text; }
        }

        public void Execute(object parameter)
        {
            _Action(parameter);
        }

        bool ICommand.CanExecute(object parameter)
        {
            return _canExecute;
        }

        public event EventHandler CanExecuteChanged;

        private void RaiseCanExecuteChanged()
        {
            var canExecuteChanged = CanExecuteChanged;
            if (canExecuteChanged != null) canExecuteChanged(this, EventArgs.Empty);
        }

        private readonly Action<object> _Action;
        private bool _canExecute = true;
        private readonly string _Text;
    }
}
